package br.com.caelum.vraptor.controller;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.components.UsuarioWeb;
import br.com.caelum.vraptor.components.VendedorWeb;
import br.com.caelum.vraptor.dao.VendedorDAO;
import br.com.caelum.vraptor.entidades.Usuario;
import br.com.caelum.vraptor.entidades.Vendedor;

@Controller
public class AutenticacaoController {
	
	@Inject
	private Result result;
	@Inject
	private VendedorDAO vendedordao;
	
	@Inject
	private VendedorWeb vendedorWeb;
	
	public AutenticacaoController() {
		// TODO Auto-generated constructor stub
	}
	
	public void login (){
		result.redirectTo(IndexController.class).index();
	}
	
	public void logout(){
		vendedorWeb.logout();
		
		result.redirectTo(IndexController.class).index();
	}
    public void logar(String nome, String senha){
    	Vendedor v = vendedordao.find(nome, senha);
    	if(v==null){result.redirectTo(this).login();}
    	vendedorWeb.login(v);
    	result.redirectTo(IndexController.class).index();
    	
    }
}
